<style>
    #obj-add {
        padding: 20px 25px 25px 0;
    }

    #obj-add .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="obj-add">
    <form class="layui-form" action="" lay-filter="obj-add-form">
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">图标标题：</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" name="iconTitle" class="layui-input" lay-verify="required" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">别名：</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" name="iconName" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">图标数据：</label>
            <div class="layui-input-block">
                <input type="hidden" name="iconData" id="iconData">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传图标：</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test1">上传</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1">
                    <p id="demoText"></p>
                </div>
            </div>
        </div>

        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="obj-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>

<script>
    layui.use(['febs', 'form', 'formSelects', 'validate', 'treeSelect', 'upload'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            formSelects = layui.formSelects,
            upload = layui.upload,
            treeSelect = layui.treeSelect,
            form = layui.form,
            $view = $('#obj-add'),
            validate = layui.validate;

        form.verify(validate);
        form.render();

        formSelects.render();
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/icon/iconUpload' //改成您自己的上传接口
            , before: function (obj) {

                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    console.log(result);
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 200) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#iconData").val(res.data)
                var demoText = $('#demoText');
                demoText.append('<span style="color: green;">上传成功</span>');
                return layer.msg('上传成功');
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        })


        form.on('submit(obj-add-form-submit)', function (data) {
            febs.post(ctx + 'icon', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增成功');
                $('#febs-obj').find('#query').click();
            });
            return false;
        });
    });
</script>